require "serialize_accessors/version"

module SerializeAccessors
  def serialize field
    super if defined?(super)
  end

  def serialize_accessors(field, *args)
    args.each do |method_name|
      define_method "#{method_name}" do
        eval "
          (self.#{field} || {})[:#{method_name}]
        "
      end
      define_method "#{method_name}=" do |value|
        eval "
          self.#{field} ||= {}
          self.#{field}[:#{method_name}] = value
        "
      end
    end
    serialize field
  end
end